package ratelimit

import (
	"context"
	"fmt"
	"jk-webook/internal/service/sms"
	"jk-webook/pkg/ratelimit"
)

// Service 装饰器模式
type Service struct {
	svc     sms.Service
	limiter ratelimit.Limiter
}

func NewService(svc sms.Service, limiter ratelimit.Limiter) *Service {
	return &Service{}
}
func (s Service) Send(ctx context.Context, tpl string, args []string, number ...string) error {
	limited, err := s.limiter.Limited(ctx, "sms:tencent")
	if err != nil {
		return fmt.Errorf("短信服务是否限流:%w", err)
	}
	if limited {
		return fmt.Errorf("出发了限流")

	}
	//加一些代码，特性
	err = s.svc.Send(ctx, tpl, args, number...)
	return err
}
